#include <iostream>
#include <pthread.h>
#include <unistd.h>

int tickets = 5000;

void GetTickets(pthread_mutex_t *ptr_mutex)
{
    while (true)
    {
        pthread_mutex_lock(ptr_mutex);
        if (tickets != 0)
        {
            std::cout << "thraead:" << gettid() << "  get a ticket, number is " << tickets << std::endl;
            tickets--;
            pthread_mutex_unlock(ptr_mutex);
        }
        else
            break;
    }
}

void *headler(void *ptr)
{
    GetTickets((pthread_mutex_t *)ptr);
}

int main()
{

    pthread_mutex_t mutex;
    pthread_mutex_init(&mutex, nullptr);

    pthread_t c1, c2, c3, c4, c5;
    pthread_create(&c1, nullptr, headler, &mutex);
    pthread_create(&c2, nullptr, headler, &mutex);
    pthread_create(&c3, nullptr, headler, &mutex);
    pthread_create(&c4, nullptr, headler, &mutex);
    pthread_create(&c5, nullptr, headler, &mutex);

    pthread_join(c1, nullptr);
    pthread_join(c2, nullptr);
    pthread_join(c3, nullptr);
    pthread_join(c4, nullptr);
    pthread_join(c5, nullptr);

    return 0;
}